package com.blakebarrett.windows
{
	import com.blakebarrett.components.ImageRegion;
	import com.blakebarrett.events.ImageLoadedEvent;
	import com.blakebarrett.model.Model;
	import com.blakebarrett.model.media.MediaInfo;
	import com.blakebarrett.utils.ImageUtil;
	
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.utils.ByteArray;
	
	import mx.core.Window;
	import mx.events.DragEvent;
	import mx.events.FlexEvent;
	import mx.managers.DragManager;
	
	public class StereoPairWindow extends Window
	{
		public function StereoPairWindow()
		{
			super();
			this.addEventListener(DragEvent.DRAG_ENTER, onDragEnter);
			this.addEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete);
			this.addEventListener(MouseEvent.MOUSE_DOWN, setActiveStereogram );
			this.setStyle("horizontalGap",0);
			this.layout = "horizontal";
			this.width = 800;
			this.height = 600;
		}
		
		public var left : ImageRegion;
		public var right : ImageRegion;
		
		public var file : File; // what this StereoPair will get saved into.
		public var selectedMedia : MediaInfo;
		
		private function setActiveStereogram (e:MouseEvent) : void {
			Model.getInstance().selectedStereoPair = this;
		}
		
		private function onCreationComplete (e:FlexEvent) : void {
			e.currentTarget.removeEventListener(e.type, onCreationComplete);
			left = new ImageRegion();
			right = new ImageRegion();
			
			left.percentWidth = 50;
			left.percentHeight = 100;
			
			right.percentWidth = 50;
			right.percentHeight = 100;
			
			this.addChild( left );
			this.addChild( right );
			
			setActiveStereogram(null);
		}
		
		private function onDragEnter ( e : DragEvent ) : void {
			DragManager.acceptDragDrop( this );
		}
		
		public function loadAndSplitImage ( e : Event ) : void {
			var imageUrl : String = e.currentTarget.url;
			
			left.addEventListener(ImageLoadedEvent.IMAGE_LOADED_EVENT, onImageLoadComplete);
			left.load( imageUrl );
			right.addEventListener(ImageLoadedEvent.IMAGE_LOADED_EVENT, onImageLoadComplete);
			right.load( imageUrl );
							
		}
		
		private function onImageLoadComplete ( e : ImageLoadedEvent ) : void {
			
			e.currentTarget.removeEventListener(ImageLoadedEvent.IMAGE_LOADED_EVENT, onImageLoadComplete);
			
			var currentImage : ImageRegion = (e.currentTarget as ImageRegion);
			currentImage.mediaInfo.scaleToFit();
			
			if ( currentImage == right ){
				currentImage.mediaInfo.currentXOffset = currentImage.mediaInfo.maximumX;
				currentImage.img.x = -(currentImage.mediaInfo.originalWidth / 2);
			} else {
				currentImage.mediaInfo.currentXOffset = 0;
				currentImage.mediaInfo.originalWidth = currentImage.mediaInfo.originalWidth / 2;
			}
		}
		
		public function swap() : void {
			this.addChild( this.removeChildAt(0) );
		}
		
		public function deSelect () : void {
			left.deSelect();
			right.deSelect();
		}
		
		public function save () : void {
			deSelect();
			if (! file ) { 
				browseAndSave();
			} else {
				saveBitmapData();
			}
		}
		
		public function browseAndSave () : void {
			var dest : File = new File ();
			dest.addEventListener(Event.SELECT, selectedDestination );
			dest.browseForSave("Save as...");
		}
		
		private function selectedDestination ( e : Event ) : void {
			e.currentTarget.removeEventListener( e.type, selectedDestination );
			file = e.target as File;
			saveBitmapData();
		}
		
		public function saveBitmapData () : void {
			
			if ( !file.extension ) {
				var fileNativePath : String = file.nativePath.toString();
				var lastFourCharacters : String = fileNativePath.substring( fileNativePath.length - 4, fileNativePath.length );
				// if you actually titled your file "myGreatStereogram.jPg" then lets preserve your ".jPg" by not modifying it
				if (lastFourCharacters.toLowerCase() != ".jpg" ) {
					file.nativePath += ".jpg";
				}
			}
			
			this.title = file.name;
			
			var stream: FileStream = new FileStream ();
			
			try{
				stream.open( file, FileMode.WRITE );
				var bmpData : BitmapData = this.bitmapData;
				var _encodedBmp : ByteArray = ImageUtil.encode( bmpData );
				stream.writeBytes( _encodedBmp );
				stream.close();
			} catch ( e : Error ) {
				trace ("FAIL! " + e.message.toString() );
			}
		}
		
		public function get bitmapData () : BitmapData {
			var _bitmapData : BitmapData = new BitmapData( this.width, (this.height - this.viewMetrics.bottom) );
			_bitmapData.draw( this );
			return _bitmapData;
		}

	}
}